<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-11-11 13:00:00
 * @modified         2016-11-11 13:00:00
 */

class ControllerSmsSms extends Controller
{
    public function index()
    {
        $json = array();

        //$this->load->model('sms/sms');
        $tel = $this->request->post['tel'];

        if ((utf8_strlen($tel) != 11) || !preg_match('/^\d{10,13}$/', $tel)) {
            $json = array(
                'error' => $this->language->get('error_tel'),
            );
        }
        $this->load->language('sms/sms');
        $this->load->model('account/customer');
        if ($this->model_account_customer->getTotalCustomersByTelephone($tel)) {
            $json = array(
                'error' => $this->language->get('error_tel_exists'),
            );
        }

        if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('register', (array)$this->config->get('config_captcha_page'))) {
            $this->load->language('extension/captcha/basic_captcha');
            if (!isset($this->session->data['captcha']) || empty($this->session->data['captcha'])
                || !isset($this->request->post['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])
            ) {
                $json = array(
                    'error' => $this->language->get('error_captcha'),
                );
            }
        }

        $code = mt_rand(100000, 999999); //生成校验码
        setcookie('smscode', $code, time() + 600, '/');
        setcookie('tel', $tel, time() + 600, '/');
        //$this->session->data['smscode'] = $code;
        //$this->session->data['tel'] = $tel;

        $this->load->language('sms/sms');
        //$msg = sprintf($this->language->get('entry_smsmsg'), $this->config->get('config_name'), $code);
        $msg = sprintf($this->language->get('entry_smsmsg'), $code);
        $this->log_result('发送消息：'.$msg);

        if (!$json) {
            $sms = new Sms($this->config->get('config_c123_ac'), $this->config->get('config_c123_authkey'), $this);
            $sms->setParams($msg, $tel);
            $ret = $sms->send();
            if ($ret === true) {
                $this->log_result('发送成功');
                $json = array(
                    'status' => 'success',
                );
            } else {
                $this->log_result('发送失败,'.$ret);
                $json = array(
                    'status' => 'fail',
                    'msg' => 'Fail: '.$ret,
                );
            }
        }

        $this->response->setOutput(json_encode($json));
    }

    /*  日志消息,把反馈的参数记录下来*/
    public function log_result($word)
    {
        $fp = fopen(DIR_LOGS.'log_sms_'.strftime('%Y%m%d', time()).'.txt', 'a');
        flock($fp, LOCK_EX);
        fwrite($fp, $word.'::Date：'.strftime('%Y-%m-%d %H:%I:%S', time())."\t\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}
